#pragma once
#include "eventloop.h"
#include "socket.h"
#include "inetaddress.h"
#include "channel.h"
#include<functional>

class EventLoop;

class Acceptor
{
private:
    std::shared_ptr<EventLoop> loop;
    std::shared_ptr<Socket> sock;
    // InetAddress *addr;
    // Channel *acceptChannel;
    std::function<void(std::shared_ptr<Socket>)> newConnectionCallback;
public:
    Acceptor(std::shared_ptr<Socket> _sock, std::shared_ptr<EventLoop> _loop);
    ~Acceptor();
    void acceptConnection();
    void setNewConnectionCallback(std::function<void(std::shared_ptr<Socket>)> cb);
};

Acceptor::Acceptor(std::shared_ptr<Socket> _sock, std::shared_ptr<EventLoop> _loop): sock(_sock), loop(_loop)
{
}

Acceptor::~Acceptor()
{
}

void Acceptor::acceptConnection(){
    std::cout<<"accept one"<<std::endl;
    std::unique_ptr<InetAddress> clnt_addr = std::make_unique<InetAddress>();  
    std::shared_ptr<Socket> clnt_sock = std::make_shared<Socket>(sock->accept(std::move(clnt_addr)));   
    newConnectionCallback(clnt_sock);
}

void Acceptor::setNewConnectionCallback(std::function<void(std::shared_ptr<Socket>)> cb){
    newConnectionCallback = cb;
}
